/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package validatortest.validator;

import java.util.Arrays;
import javax.swing.JOptionPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Imam Habib Pamungkas <imamhabibpamungkas@gmail.com>
 */
public class ValidatorFactory {

    public static PlainDocument getDocument(String docType) {
        if (docType == null) {
            return null;
        }
        if (docType.equalsIgnoreCase("digit")) {
            return new NumberValidator();
        }
        return null;
    }

    public static PlainDocument getDocument(String docType, Integer[] constraint, String message) {
        if (docType.equalsIgnoreCase("constraint")) {
            return new ConstraintValidator(constraint, message);
        }
        if (docType.equalsIgnoreCase("depentSum")) {
            return new DepentSumValidator(constraint, message);
        }
        return null;
    }

}

class DepentSumValidator extends PlainDocument {

    private Integer[] posisiText;
    private String message;

    public DepentSumValidator(Integer[] posisiText, String message) {
        this.message = message;
        this.posisiText = posisiText;
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        super.insertString(offs, str, a);
        int sum = 0;
        for (int i = 0; i < posisiText.length; i++) {
            int j = Integer.parseInt(FormControl.getListForm().get(posisiText[i]).getText());
            sum += j;
        }
        String length = Integer.toString(sum);
        System.out.println(sum + " " + length);
        if (length.length() == getLength()) {
            if (getText(0, length.length()).equalsIgnoreCase(length)) {
                FormControl.next();
            } else {
                JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
                FormControl.getCurrentText().setValid(false);
            }
        }
    }

}

class NumberValidator extends PlainDocument {

    @Override
    public void insertString(int arg0, String str, AttributeSet arg2) throws BadLocationException {
        if (ConstraintValidator.AllValidation.isDigit(str)) {
            super.insertString(arg0, str, arg2);
        }
    }
}

class ConstraintValidator extends PlainDocument {

    private Integer[] constraint;
    private String message;

    public ConstraintValidator(Integer[] constraint, String message) {
        this.constraint = constraint;
        this.message = message;
        //  System.out.println("isi constraint "+constraint[1]);
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        // System.out.println(FormControl.getCurrentText().getPosisi());
        if (ConstraintValidator.AllValidation.isDigit(str) && getLength() != FormControl.getCurrentText().getLength()) {
            super.insertString(offs, str, a);
        } else {
            JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
        }

        if (getLength() == FormControl.getCurrentText().getLength()) {
            Integer input = Integer.parseInt(getText(0, FormControl.getCurrentText().getLength()));
            if (Arrays.asList(constraint).contains(input)) {
                FormControl.getCurrentText().setValid(true);
                FormControl.next();
            } else {
                // remove(0, getLength());
                JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
                FormControl.getCurrentText().setValid(false);
            }
        }
    }

    class ToUpperCase extends DocumentFilter {

    }

    static class AllValidation {

        static boolean isDigit(String str) {
            if (Character.isDigit(str.charAt(0))) {
                return true;
            } else {
                //    JOptionPane.showMessageDialog(null, "Hanya Digit", "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
    }

}
